package modelController.sessionController;

import entities.Knowledge;
import java.io.Serializable;
import java.util.List;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

/**
 *
 * @author Administrator
 */
@Named("robotAnswerController")
@SessionScoped
public class RobotAnswerController implements Serializable {

    @Inject
    modelController.applicationController.KnowledgeController applicationKnowledgeController;
    private String question = "";
    private String answer = "This is my answer!";

    /**
     * @return the question
     */
    public String getQuestion() {
        return question;
    }

    /**
     * @param question the question to set
     */
    public void setQuestion(String question) {
        this.question = question;

    }

    /**
     * @return the answer
     */
    public String getAnswer() {
        if (question.trim().length() > 0) {
            //从数据库Knowledge中查询获得相关的知识点
            StringBuilder sqlString = new StringBuilder();
            sqlString.append("SELECT * FROM LEARNING.KNOWLEDGE where name='");
            sqlString.append(question);
            sqlString.append("' or locate(details,'");
            sqlString.append(question);
            sqlString.append(">0");
            List<Knowledge> knowledges = applicationKnowledgeController.getQueryResultList(sqlString.toString());
            if (!knowledges.isEmpty()) {
                answer = knowledges.get(0).getDetails();
            } else {
                answer = "No answer";
            }
            return answer;
        } else {
            return "";
        }
    }

    /**
     * @param answer the answer to set
     */
    public void setAnswer(String answer) {
        this.answer = answer;
    }
}
